﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL = AccessDAL;
using DBUtility;

namespace BLL
{
    public class News
    {
        DAL.NewsTableAdapters.NewsTableAdapter DN =
            new DAL.NewsTableAdapters.NewsTableAdapter();

        MODEL.News _MN = new MODEL.News();
        MODEL.NewsClass MNC = new MODEL.NewsClass();
        MODEL.NewsClass _MNC = new MODEL.NewsClass();

        BLL.NewsClass BNC = new NewsClass();


        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MN"></param>
        /// <returns></returns>
        public MODEL.News GetDataByID(MODEL.News MN)
        {
            try
            {
                DataTable DT = DN.GetDataByID(MN.News_ID);

                _MN.News_ClassCode = DT.Rows[0]["News_ClassCode"].ToString();
                _MN.News_ClassID = Convert.ToInt32(DT.Rows[0]["News_ClassID"]);
                _MN.News_Content = DT.Rows[0]["News_Content"].ToString();
                _MN.News_DateTime = Convert.ToDateTime(DT.Rows[0]["News_DateTime"]);
                _MN.News_Hits = Convert.ToInt32(DT.Rows[0]["News_Hits"]);
                _MN.News_ID = Convert.ToInt32(DT.Rows[0]["News_ID"]);
                _MN.News_IsDel = Convert.ToBoolean(DT.Rows[0]["News_IsDel"]);
                _MN.News_IsLock = Convert.ToBoolean(DT.Rows[0]["News_IsLock"]);
                _MN.News_MasterID = Convert.ToInt32(DT.Rows[0]["News_MasterID"]);
                _MN.News_Order = Convert.ToInt32(DT.Rows[0]["News_Order"]);
                _MN.News_Title = DT.Rows[0]["News_Title"].ToString();

                return _MN;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加信息
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="MN"></param>
        public void Add(MODEL.News MN)
        {
            try
            {
                MNC.NC_ID = MN.News_ClassID;
                _MNC = BNC.GetDataByID(MNC);

                object Temp_OrderNum = DN.GetMaxOrder();
                int OrderNum;
                string ClassCode = _MNC.NC_ClassCode;

                if (Temp_OrderNum == null)
                {
                    OrderNum = 1;
                }
                else
                {
                    OrderNum = Convert.ToInt32(Temp_OrderNum) + 1;
                }

                DataTable DT_Check = DN.GetDataByTitle_ClassID(MN.News_Title, MN.News_ClassID);

                if (DT_Check.Rows.Count != 0)
                {
                    throw new Exception("同一类别下，不能有重复标题");
                }

                DN.Add(MN.News_Title, MN.News_ClassID, ClassCode, MN.News_Content, MN.News_DateTime,
                    MN.News_Hits, MN.News_MasterID, MN.News_IsLock, MN.News_IsDel, OrderNum);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改信息
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="MN"></param>
        public void Mod(MODEL.News MN)
        {
            try
            {
                MNC.NC_ID = MN.News_ClassID;
                _MNC = BNC.GetDataByID(MNC);
                string ClassCode = _MNC.NC_ClassCode;

                DN.Mod(MN.News_Title, MN.News_ClassID, ClassCode, MN.News_Content, MN.News_IsLock, MN.News_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除信息
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="MN"></param>
        public void Del(MODEL.News MN)
        {
            try
            {
                DN.DelByID(MN.News_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 标记是否删除
        /// <summary>
        /// 标记是否删除
        /// </summary>
        /// <param name="MN"></param>
        public void MarkIsDel(MODEL.News MN)
        {
            try
            {
                string SQLstr = "UPDATE [News] SET [News_IsDel]=NOT [News_IsDel] WHERE [News_ID]=" + MN.News_ID;

                AccHelper.ExecuteSQL(SQLstr);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 标记是否锁定
        /// <summary>
        /// 标记是否锁定
        /// </summary>
        /// <param name="MN"></param>
        public void ModIsLock(MODEL.News MN)
        {
            try
            {
                string SQLstr_ModIsLock = "UPDATE [News] SET [News_IsLock]=NOT [News_IsLock] WHERE [News_ID]=" + MN.News_ID;

                AccHelper.ExecuteSQL(SQLstr_ModIsLock);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 点击加1
        /// <summary>
        /// 点击加1
        /// </summary>
        /// <param name="MN"></param>
        public void HitsPlus1(MODEL.News MN)
        {
            try
            {
                DN.HitsPlus1(MN.News_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
